﻿using System.Text.RegularExpressions;
using System.Xml;

namespace TWA.TFS
{
    /// <summary>
    /// An event data container and utility methods for extracting specific fields.
    /// </summary>
    internal class EventData
    {
        internal XmlElement Raw { get; set; }

        internal EventData (string eventXml)
        {   
            Logger.Log ("eventXML: " + eventXml);

            var doc = new XmlDocument ();
            doc.LoadXml (eventXml);

            Raw = doc.DocumentElement;
        }

        internal string WorkItemId
        {
            get
            {
                return Raw.SelectSingleNode ("CoreFields/IntegerFields/Field[ReferenceName='System.Id']/NewValue").InnerText;
            }
        }

        internal string WorkItemTitle
        {
            get
            {
                return Raw.SelectSingleNode ("CoreFields/StringFields/Field[ReferenceName='System.Title']/NewValue").InnerText;
            }
        }

        internal string WorkItemType
        {
            get
            {
                return Raw.SelectSingleNode ("CoreFields/StringFields/Field[ReferenceName='System.WorkItemType']/NewValue").InnerText;
            }
        }

        internal string PortfolioProject
        {
            get { return Raw.SelectSingleNode ("PortfolioProject").InnerText; }
        }

        internal string AreaPath
        {
            get { return Raw.SelectSingleNode ("AreaPath").InnerText; }
        }

        internal string ChangeType
        {
            get { return Raw.SelectSingleNode ("ChangeType").InnerText; }
        }

        internal string CreatedBy
        {
            get
            {
                return Raw.SelectSingleNode ("CoreFields/StringFields/Field[ReferenceName='System.CreatedBy']/NewValue").InnerText;
            }
        } 

        internal string ChangedBy
        {
            get
            {
                return Raw.SelectSingleNode ("CoreFields/StringFields/Field[ReferenceName='System.ChangedBy']/NewValue").InnerText;
            }
        }

        internal string AssignedTo
        {
            get
            {
                return Raw.SelectSingleNode ("CoreFields/StringFields/Field[ReferenceName='System.AssignedTo']/NewValue").InnerText;
            }
        }

        internal string OldAssignedTo
        {
            get
            {
                return Raw.SelectSingleNode ("CoreFields/StringFields/Field[ReferenceName='System.AssignedTo']/OldValue").InnerText;
            }
        }  

        /// <summary>
        /// Determine if this event was generated by a changeset checkin. Test for the existence 
        /// of the System.History value and parse the comment for the text "Associated with changeset 1234."
        /// </summary>
        internal bool IsChangesetCheckin
        {
            get 
            { 
                XmlNode historyNode = Raw.SelectSingleNode ("TextFields/TextField[ReferenceName='System.History']/Value");

                if (historyNode != null)
                {
                    string history = historyNode.InnerText;

                    const string HISTORY_PATTERN = @"^Associated \s with \s changeset \s [0-9]* [.]$";
                    Match matches = Regex.Match (history, HISTORY_PATTERN, RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

                    if (matches.Success)
                    {
                        return true;
                    }
                }
                
                return false;
            }
        }
    }
}